import os
def filter_lines(input_file, output_file):
    """
    从输入文件中筛选包含 "beta" 和 "train matrix" 的行，并保存到输出文件。

    参数:
    input_file (str): 输入文件名
    output_file (str): 输出文件名
    """
    try:
        with open(input_file, 'r', encoding='utf-8') as infile, open(output_file, 'w', encoding='utf-8') as outfile:
            for line in infile:
                if "beta" in line and "val matrix" in line:
                    outfile.write(line)
        print(f"已成功将包含 'beta' 和 'train matrix' 的行从 {input_file} 保存到 {output_file}")
    except FileNotFoundError:
        print(f"错误: 文件 {input_file} 未找到.")
    except Exception as e:
        print(f"发生错误: {e}")

dir_name = "logs/bs16_epoch100_lr0.0001/TrainerDR_20250315_resnexxt50_all"
# 示例用法
input_file_name = os.path.join(dir_name, "training_log.txt")  # 替换为你的输入文件名
output_file_name = os.path.join(dir_name, "val log.txt")
filter_lines(input_file_name, output_file_name)